package com.panopset.sf.apps;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import com.panopset.centralen.resource.ImageResource;
import com.panopset.game.blackjack.BlackjackConfig;
import com.panopset.game.card.Card;
import com.panopset.game.card.CardDefinition.Suit;
import com.panopset.game.card.CardLocation;
import com.panopset.game.card.CardPainter;
import com.panopset.game.card.Shoe;

public class SwingCardPainter {

    private final CardPainter cp;

    public Suit[] getOrder() {
        return cp.getOrder();
    }

    public SwingCardPainter(BlackjackConfig config) {
        cp = new CardPainter(config, mapImg.getWidth(null), mapImg.getHeight(null));
    }

    public void paintCard(Graphics g, CardLocation cl, Color hlt) {
        paintCard(g, cl.getCard(), cl.x, cl.y, hlt);
    }

    public void paintCard(Graphics g, CardLocation cl) {
        paintCard(g, cl.getCard(), cl.x, cl.y);
    }

    public void paintCard(Graphics g, Card card, int x, int y) {
        paintCard(g, card, x, y, Color.BLACK);
    }

    public void paintCard(Graphics g, Card card, int x, int y, Color hlt) {
        int[] b = cp.getPaintDimensions(card, x, y);
        int arc = 10;
        if (card.isShowing()) {
            g.drawImage(mapImg, b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
                    null);
        } else {
            if (Shoe.isBlue()) {
                g.setColor(Color.BLUE);
            } else {
                g.setColor(Color.RED);
            }
            g.fillRoundRect(b[0], b[1], getCardWidth(), getCardHeight(), arc,
                    arc);
            for (int i = 0; i < getBorder(); i++) {
                g.drawRect(b[0] + i, b[1] + i, getCardWidth() - (i * 2),
                        getCardHeight() - (i * 2));
            }
        }
        g.setColor(hlt);
        g.drawRect(b[0], b[1], getCardWidth(), getCardHeight());
    }

    private Integer border;

    public int getBorder() {
        if (border == null) {
            if (getCardWidth() > 30) {
                border = getCardWidth() / 30;
            } else {
                border = 1;
            }
        }
        return border;
    }

    public int getCardHeight() {
        return cp.getCardHeight();
    }

    public int getCardWidth() {
        return cp.getCardWidth();
    }

    /**
     * Default card image is the one from jfitz.com.
     */
    public static final String DFT_CARD_IMAGE = "/com/panopset/game/card/jfitz.com.cards.png";

    public final static Image mapImg = new ImageResource(DFT_CARD_IMAGE)
    .getImage();

}
